package com.mashibing.internalcommon.dto;

import com.mashibing.internalcommon.constant.CommonStatusEnum;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class ResponseResult<T> {

    private int code;
    private String message;
    private T data;

    /**
     * 成功响应的方法
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<Void> success(){
        ResponseResult<Void> result = new ResponseResult<>();
        result.setCode(CommonStatusEnum.SUCCESS.getCode());
        result.setMessage(CommonStatusEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 成功响应的方法
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult <T> success(T data) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(CommonStatusEnum.SUCCESS.getCode());
        result.setMessage(CommonStatusEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 失败：统一的失败
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult <T> fail(T data){
        ResponseResult<T> result = new ResponseResult<>();
        result.setData(data);
        return result;
    }

    /**
     * 失败：自定义失败 错误码和提示信息
     * @param code
     * @param message
     * @return
     */
    public static <T> ResponseResult<T> fail(int code,String message,T data){
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMessage(message);
        if (null != data) {
            result.setData(data);
        }
        return result;
    }

}
